home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / graphics / tierra40.arj / TIERRA / DISKBANK.C < prev    next >
C/C++ Source or Header  |  1992-09-09  |  5KB  |  204 lines

  1. /* diskbank.c   9-9-92  disk genebank manager for the Tierra Simulator */
  2. /* Tierra Simulator V4.0: Copyright (c) 1991, 1992 Tom Ray & Virtual Life */
  3.  
  4. #ifndef lint
  5. static char sccsid[] = "@(#)diskbank.c    1.0        7/21/92";
  6.  
  7. #endif
  8.  
  9. #include "license.h"
  10. #include "tierra.h"
  11. #include "extern.h"
  12. #include <errno.h>
  13. #include <sys/types.h>
  14. #ifdef unix
  15. #include <dirent.h>
  16. #endif               /* unix */
  17. #ifdef __TURBOC__
  18. #include <dir.h>
  19. #include <dos.h>
  20. #define d_name ff_name
  21. #endif               /* __TURBOC__ */
  22.  
  23. #ifdef MEM_CHK
  24. #include <memcheck.h>
  25. #endif
  26.  
  27. void Inject(g, size, sad, tol, disk, rrpi)
  28. FpInst  g;     /* pointer to genome */
  29. I32s    size;  /* size of genome */
  30. I32s    sad;   /* suggested address for placement of genome */
  31. I32s    tol;   /* tolerance placement of genome */
  32. I32s    disk;  /* 1 = this genome comes from the disk */
  33. float   *rrpi; /* reap rand prop for injection */
  34. {   float   tReapRndProp = ReapRndProp;
  35.     I32s    osize, j, k;
  36.     I16s   gi;
  37.     Pcells  ce;
  38.     FpInst  si;
  39.     Event    SizGen;
  40.     GlIndex  GiHash;
  41.  
  42.     ce = GetFreeCell();   /* get a cell structure */
  43.     ce->ld = 1;
  44.     ce->d.gen.size = ce->mm.s = size;
  45.     ReapRndProp = *rrpi;   /* allocate the needed memory */
  46.     while ((ce->mm.p = MemAlloc(size, sad, tol)) < 0)
  47.         reaper(1,sad);
  48.     ReapRndProp = tReapRndProp;
  49.     ce->c.ip = ce->mm.p;
  50.     si = ce->d.genome = soup + ce->c.ip;
  51.  
  52.     if (ce == &cells[0][2])
  53.     {   ce->q.p_reap = TopDummy->q.this;
  54.         ce->q.n_reap = BottomDummy->q.this;
  55.         TopDummy->q.n_reap = ce->q.this;
  56.         BottomDummy->q.p_reap = ce->q.this;
  57.     }
  58.     else
  59.     {   EntBotSlicer(ce);
  60.         EntBotReaper(ce);
  61.     }
  62.     ce->d.is = 1;
  63.  
  64.     for (j = 0; j < size; j++, si++) /* put genome in soup */
  65. #if PLOIDY == 1
  66.         si[0] = g[j];
  67. #else  /* PLOIDY == 1 */
  68.     for (k = 0; k < PLOIDY; k++)
  69.         si[0][k] = g[j][k];
  70. #endif /* PLOIDY == 1 */
  71.  
  72.     if (GeneBnker) /* determine genotype, record in genebanker */
  73.     {   GiHash = CheckGenotype(ce->d, 21);    /* check .gen files */
  74.         ce->d.hash = GiHash.si;
  75.         ce->d.gi = gi = GiHash.gi;
  76.         strcpy(ce->d.gen.label, Int2Lbl(GiHash.gi));
  77.         ce->d.parent = sl[size]->g[gi]->parent;
  78.         SizGen = DivGenBook(NULL, ce, InstExe, reaped, 0, 0, disk);
  79.         NumGenotypes += SizGen.i;
  80.         NumSizes += SizGen.m;
  81.     }
  82.     OutDisk((I32s) 'b', ce);
  83. }
  84.  
  85. void InjectFromBank(crit, sad, tol)
  86.     I8s     *crit;
  87.     I32s    sad;   /* suggested address for placement of genome */
  88.     I32s    tol;   /* tolerance placement of genome */
  89. {
  90.     float  rrpi = 1;
  91.     I32s   size;
  92.     GList  *g;
  93.  
  94.     g = GetAGen(crit);
  95.     sscanf(crit, "%4ld", &size);
  96.     Inject(g->genome, size, sad, tol, 1, &rrpi);
  97.     FreeGen(g);
  98. }
  99.  
  100. void FreeGen(g)
  101.     GList  *g;
  102. {   if (g)
  103.     {   if (g->genome)
  104.         {   tfree(g->genome);
  105.             g->genome = NULL;
  106.         }
  107.         if (g->gbits)
  108.         {   tfree(g->gbits);
  109.             g->gbits = NULL;
  110.         }
  111.         tfree(g);
  112.         g = NULL;
  113.     }
  114. }
  115.  
  116. GList * GetAGen(crit)
  117.     I8s     *crit;
  118. {
  119.     I32s   size;
  120.     I16s   n;
  121.     GList  *g;
  122.     char   cpath[128], gen[4];
  123.     FILE   *fp;
  124.     head_t head;
  125.     indx_t *indx, *tindx, gindx;
  126.  
  127.     sscanf(crit, "%4ld%3s", &size, gen);
  128.     sprintf(cpath, "%s%04ld.gen", GenebankPath, size);
  129.     if (!(fp = open_ar(cpath, size, GFormat, 0)))
  130.     {   FEError(-1306,EXIT,NOWRITE,
  131.             "Tierra InjectFromBank() unable to open genome file %s\n",cpath);
  132.     }
  133.     head = read_head(fp);
  134. #ifdef __TURBOC__
  135.     indx = &gindx;
  136.     n = find_gen(fp, indx, gen, head.n);
  137.     tindx = indx;
  138. #else  /* __TURBOC__ */
  139.     indx = read_indx(fp, &head);
  140.     n = find_gen(fp, indx, gen, head.n);
  141.     tindx = &indx[n];
  142. #endif /* __TURBOC__ */
  143.     g = get_gen(fp, &head, tindx, n);
  144.     fclose(fp);
  145. #ifndef __TURBOC__
  146.     if (indx)
  147.     {   thfree(indx);
  148.         indx = NULL;
  149.     }
  150. #endif  /* __TURBOC__ */
  151.     return g;
  152. }
  153.  
  154. void extract(ce)
  155.     Pcells ce;
  156. {
  157.     I16u i, j;
  158.     I32s size;
  159.     I32s ip;
  160.     Pgl g;
  161.     FILE *fp;
  162.     head_t head;
  163.     indx_t *indx, gindx;
  164.  
  165.     if (!GeneBnker)
  166.         return;
  167.     isolate = 0;
  168. #ifdef IBM3090
  169.     sprintf(Buff, "%04ld.gen.d", ce->d.gen.size);
  170. #else
  171.     sprintf(Buff, "%s%04ld.gen", GenebankPath, ce->d.gen.size);
  172. #endif
  173.     size = ce->d.gen.size;
  174.     g = sl[size]->g[ce->d.gi];
  175.     sprintf(ExtrG, "%04ld%s @ %ld", g->gen.size, g->gen.label, g->pop);
  176. #if FRONTEND == STDIO
  177. sprintf(mes[0], "extract: %s", ExtrG);
  178. FEMessage(1,mes);
  179. #else /* FRONTEND == STDIO */
  180.     if (Log) fprintf(tfp_log, "ex = %s\n", ExtrG);
  181. #endif /* FRONTEND == STDIO */
  182.  
  183. /* DAN open an archive, if it does not exist, create it */
  184.     if (!(fp = open_ar(Buff, ce->d.gen.size, GFormat, -1)))
  185.     {   FEError(-200,EXIT,NOWRITE,
  186.             "Tierra extract() Unable to open extract file %s",Buff);
  187.     }
  188.     head = read_head(fp);
  189. #ifdef __TURBOC__
  190.     indx = &gindx;
  191. #else  /* __TURBOC__ */
  192.     indx = read_indx(fp, &head);
  193. #endif /* __TURBOC__ */
  194.     add_gen(fp, &head, &indx, g);
  195. #ifndef __TURBOC__
  196.     if (indx)
  197.     {   thfree(indx);
  198.         indx = NULL;
  199.     }
  200. #endif /* __TURBOC__ */
  201.     fclose(fp);
  202.     NumGenDG++;
  203. }
  204.